/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.com.ugto.caadi.asistencias.sql;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.ResourceBundle;
import mx.com.ugto.caadi.asistencias.bean.Usuario;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import mx.com.ugto.caadi.asistencias.bean.Rol;

/**
 *
 * @author Maestria
 */
public class UsuarioSQL {

    public static void insertarUsuario(Usuario u) {
        PreparedStatement pst = null;
        Connection cn = null;
        try {
            String key = ResourceBundle.getBundle("mx/com/ugto/caadi/asistencias/resources/seguridad").getString("key");
            pst = cn.prepareStatement("INSERT INTO usuario VALUES(null,?,AES_ENCRYPT(?,'" + key + "'),?,null,null)");
            pst.setString(1, u.getStrNombre());
            pst.setString(2, u.getStrPassword());
            pst.setInt(3, u.getRol().getNumRolId());
        } catch (SQLException ex) {
            ex.printStackTrace();
        } finally {
            if (pst != null) {
                try {
                    pst.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            if (cn != null) {
                try {
                    cn.close();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        Usuario u = new Usuario();
        u.setStrNombre("rolandopalermo");
        String pass = "holamundo";
//        u.setStrPassword(pass.getBytes("UTF-8"));
        u.setStrPassword(pass);
        Rol rol = new Rol();
        rol.setNumRolId(1);
        u.setRol(rol);
        insertarUsuario(u);
    }
}
